package com.etnet.streamserver.view.common;

import java.io.IOException;
import java.util.Calendar;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;

public class GlobalResources {
    public static final String VERSION = "Stream Server Viewer  Version: 0.1.0";
    private static Logger logger = LoggerFactory.getLogger(GlobalResources.class);
    private static Properties fieldIDMapping = new Properties();
    private static Set<String> fieldIDs = new HashSet<String>();
    private static Properties globalDefine = new Properties();
    public static final String INDICES_PREFIX = "HSIS";
    public static final String GLOBAL_PREFIX = "GLOBAL";
    public static final String FOREX_PREFIX = "FOREX";
    public static final String ASHARE_PERFIX = "A";
    public static final String DEFAULT_PREFIX_SEPARATOR = ".";
    public static final String DEFAULT_INTERVAL_SEPARATOR = "_";
    public static final String DEFAULT_VIRGULE_SEPARATOR = "/";
    public static final String DEFAULT_INDICES_PREFIX = "HSIS.";
    public static final String DEFAULT_GLOBAL_PREFIX = "GLOBAL.";
    public static final String DEFAULT_FOREX_PREFIX = "FOREX.";
    public static final String DEFAULT_ASHARE_PREFIX = "A.";
    public static final String DEFAULT_AOI = "HSIS.AOI";
    public static final String DEFAULT_SS_ENCODER_CHARTSET = "UTF-8";
    public static final String DEFAULT_BC_ENCODER_CHARTSET = "BIG5-HKSCS";
    public static final int DEFAULT_BROKER_SEARCH_SIZE = 50;
    public static final byte[] SYS_LOGIN_TOKEN_DES = "123456781234567812345678".getBytes();
    public static final String SYS_LOGIN_USER_NAME = "user_key";
    public static final String SYS_LOGIN_USER_IP = "user_ip";
    public static final String CONNECT_RMI_STATUS = "1";
    public static final String CONNECT_SOCKET_STATUS = "2";
    public static final String RMI_ALL_COST = "3";
    public static final String RMI_ASA_COST = "4";
    public static final String CLIENTS_RECORDS = "5";
    public static final String CLIENTS_RECORDS_CONNECTED = "0";
    public static final String CLIENTS_RECORDS_DISCONNECTED = "1";
    public static final String CLIENTS_RECORDS_REJECTED = "2";
    public static final String SYS_SOCKET_LINE_CTRL = "\n";
    private static long today = buildToday(System.currentTimeMillis());

    public void setGlobalDefine(Resource resource) throws Exception {
        try {
            globalDefine.load(resource.getInputStream());
            for (Iterator localIterator = globalDefine.values().iterator(); localIterator.hasNext();) {
                Object fieldID = localIterator.next();
                fieldIDs.add(fieldID.toString());
            }
        } catch (IOException e) {
            logger.error("load global define error.", e);
            throw e;
        }
    }

    public void setFieldIdMapping(Resource resource) throws Exception {
        try {
            fieldIDMapping.load(resource.getInputStream());
            for (Iterator localIterator = fieldIDMapping.values().iterator(); localIterator.hasNext();) {
                Object fieldID = localIterator.next();
                fieldIDs.add(fieldID.toString());
            }
        } catch (IOException e) {
            logger.error("load fieldIDMapping error.", e);
            throw e;
        }
    }

    public static Set<String> getFieldIDs() {
        return fieldIDs;
    }

    public static String getFieldID(String fieldName) {
        return fieldIDMapping.getProperty(fieldName, fieldName);
    }

    public static String getCodeFieldID() {
        return fieldIDMapping.getProperty("S_Code");
    }

    public static long getTodayTimeInMillis() {
        return today;
    }

    private static long buildToday(long l) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(l);
        int year = calendar.get(1);
        int month = calendar.get(2);
        int date = calendar.get(5);
        calendar.clear();

        calendar.set(year, month, date, 0, 0, 0);
        return calendar.getTimeInMillis();
    }

    public static void buildTodayTime() {
        today = buildToday(System.currentTimeMillis());
    }

    public static class ProtocolDefine {
        public static final String REQUEST_UNDERLYING_SORT_KEY = "";
        public static final String REQUEST_REQID_HEARTBEAT = "0";
        public static final String REQUEST_REQID_QUOTE = "1";
        public static final String REQUEST_REQID_SORT_INDUSTRY = "2";
        public static final String REQUEST_REQID_SORT_INDEX = "3";
        public static final String REQUEST_REQID_SORT_WAR = "4";
        public static final String REQUEST_REQID_SORT_CBBC = "5";
        public static final String REQUEST_REQID_SORT_STOCK = "6";
        public static final String REQUEST_REQID_BROKER_SEARCH = "7";
        public static final String REQUEST_REQID_LOGIN = "8";
        public static final String REQUEST_REQID_NEWS = "9";
        public static final String REQUEST_REQID_SYSTEM_MESSAGE = "10";
        public static final String REQUEST_REQID_MTS_MESSAGE = "11";
        public static final String REQUEST_REQID_BROKER_SEARCH_HV = "12";
        public static final String SORT_STOCK_SECURITY = "0";
        public static final String SORT_STOCK_STK = "1";
        public static final String SORT_STOCK_CBBC = "2";
        public static final String SORT_STOCK_WAR = "3";
        public static final String SORT_STOCK_ETF = "4";
        public static final String SORT_STOCK_ADR = "5";
        public static final String SORT_STOCK_INDEX = "6";
        public static final String SORT_STOCK_INDUSTRY = "7";
        public static final String SORT_STOCK_ASHARE = "8";
        public static final String REQUEST_ACTION_TYPE_ADD = "0";
        public static final String REQUEST_ACTION_TYPE_SS = "1";
        public static final String REQUEST_ACTION_TYPE_BOTH = "2";
        public static final String REQUEST_ACTION_TYPE_REMOVE = "3";
        public static final String REQUEST_BROKER_SEARCH_TYPE_BROKER = "0";
        public static final String REQUEST_BROKER_SEARCH_TYPE_FIRM = "1";
        public static final String SINGLE_RESOURCE_WAR_NEWLISTING_TRADE_DAY = "0";
        public static final String SINGLE_RESOURCE_CBBC_NEWLISTING_TRADE_DAY = "1";
        public static final String SINGLE_RESOURCE_WAR_EXPIRING_TRADE_DAY = "2";
        public static final String SINGLE_RESOURCE_CBBC_EXPIRING_TRADE_DAY = "3";
        public static final String SINGLE_RESOURCE_AHMAPPING = "4";
        public static final String SINGLE_RESOURCE_BROKER = "5";
        public static final String SINGLE_RESOURCE_STOCK_LINK_CODE = "6";
        public static final String SINGLE_RESOURCE_SPREAD_TABLE = "7";
        public static final String SINGLE_RESOURCE_SECURITY_RELATED = "8";
        public static final String SINGLE_RESOURCE_ADR_CODE_LIST = "9";
        public static final String SINGLE_RESOURCE_STOCK_NAME = "10";
        public static final String SINGLE_RESOURCE_INDUSTRY = "11";
        public static final String SINGLE_RESOURCE_BLOCKTRADE = "12";
        public static final String SINGLE_RESOURCE_FUTURE_CODE_LIST = "13";
        public static final String SINGLE_RESOURCE_FUTURE_RELATED_LIST = "14";
        public static final String SINGLE_RESOURCE_OPTION_CODE_LIST = "15";
        public static final String SINGLE_RESOURCE_OPTION_RELATED_LIST = "16";
        public static final String SINGLE_RESOURCE_TRADING_DATE = "17";
        public static final String SINGLE_RESOURCE_TRADING_TIME = "18";
        public static final String SINGLE_RESOURCE_ETF_CODE_LIST = "19";
        public static final String SINGLE_RESOURCE_OPTION_KEYCODE_LIST = "20";
        public static final String SINGLE_RESOURCE_LOCAL_INDEX_LIST = "21";
        public static final String SINGLE_RESOURCE_GLOBAL_INDEX_LIST = "22";
        public static final String SINGLE_RESOURCE_FOREX_LIST = "23";
        public static final String SINGLE_RESOURCE_SHORT_SELL_DATE = "24";
        public static final String SINGLE_RESOURCE_RECOMMENDATOR_NAME = "25";
        public static final String SINGLE_RESOURCE_CEI_BARLIST = "26";
        public static final String SINGLE_RESOURCE_FHSI_BARLIST = "27";
        public static final String SINGLE_RESOURCE_GLOBAL_BARLIST = "28";
        public static final String SINGLE_RESOURCE_GLOBAL_ISSURE_NAME = "29";
        public static final String SINGLE_RESOURCE_CHART_CODE_LIST = "30";
        public static final String SINGLE_RESOURCE_URL = "31";
        public static final String SINGLE_RESOURCE_COMBO_CODE_LIST = "32";
        public static final String SINGLE_RESOURCE_REGISTRARINFO = "33";
        public static final String SINGLE_RESOURCE_SERVERLIST = "34";
        public static final String SINGLE_RESOURCE_SCREENLIST = "35";
        public static final int RESPONSE_SYSTEM_MESSAGE_CATEGORY_RESTART_START = 0;
        public static final int RESPONSE_SYSTEM_MESSAGE_CATEGORY_RESTART_FINISHED = 1;
        public static final int RESPONSE_OUT_INIT_BUFFER_SIZE = 1024;
        public static final String RESPONSE_RESULT_EMPTY_VALUE = "";
        public static final String STOCK2NEWS = "Stock2News";
        public static final int validProductCode = 2;
    }
}